import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Article } from "src/entities/article.entity";
import { Label } from "src/entities/label.entity";
import { User } from "src/entities/user.entity";
import { LabelInput, LabelSortInput, LabelWhereInput, PageInput } from "src/graphql";
import { LabelService } from "src/services/label/label.service";
import { UserService } from "src/services/user/user.service";

@Resolver('Label')
export class LabelResolver {
  constructor(
    private readonly labelService: LabelService,
    private readonly userService: UserService,
  ) {}

  @Query('label')
  async getLabelById(@Args() id: number) {
    return await this.labelService.findOne(id);
  }

  @Query('labels')
  async getLabels(@Args('page') page: PageInput,@Args('sort') sort:LabelSortInput,@Args('where') where:LabelWhereInput) {
    return await this.labelService.getPagination(page,sort,where);
  }

  @Mutation()
  async addLabel(@Args('label') label:LabelInput) {
    return await this.labelService.addOne(<Label>label);
  }

  @Mutation()
  async removeLabel(@Args() id: number) {
    return await this.labelService.removeOne(id);
  }

  @Mutation()
  async modifyLabel(@Args('id') id: number, @Args('label') label:LabelInput) {
    return await this.labelService.modifyOne(id,<Label>label);
  }

  @ResolveProperty(() => User,{
    name:'_user'
  })
  async user(@Parent() label){
    const { user_id } = label;
    return await this.userService.findOne(user_id);
  }

}